package com.ihr360.alps;

import com.ihr360.alps.api.AlpsProvider;
import com.ihr360.alps.api.ServiceProvider;
import com.ihr360.insurance.model.meta.DynamicHeaderCell;
import com.ihr360.insurance.service.MonthlyLedgerService;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/alps")
@DisableResponseAdvice
public class SIHFAlpsProvider implements AlpsProvider {

    @Value("${spring.application.name}")
    private String appId;

    @Autowired
    private MonthlyLedgerService monthlyLedgerService;

    @GetMapping(value = "/application", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Override
    public ApplicationDescriptor applicationDescriptor() {

        List<ServiceDescriptor> descriptors = new ArrayList<>(1);
        ServiceDescriptor serviceDescriptor = serviceDescriptor(ServiceProvider.SERVICE_ID.SIHF);
        if (serviceDescriptor != null) {
            descriptors.add(serviceDescriptor);
        }

        ApplicationDescriptor applicationDescriptor= ApplicationDescriptor.builder()
                .name("社保公积金")
                .services(descriptors)
                .id(appId).build();


        return applicationDescriptor;
    }

    @GetMapping(value = "/services/{service}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Override
    public ServiceDescriptor serviceDescriptor(@PathVariable(name = "service") String serviceId) {

        List<DynamicHeaderCell> headerCells = new ArrayList<>();

        headerCells = monthlyLedgerService.getSIHFTotalHeaderCell();

        switch (serviceId) {
            case ServiceProvider.SERVICE_ID.SIHF:
                List<PropertyDescriptor> propertyDescriptors = headerCells.stream().map(c ->
                                PropertyDescriptor.builder()
                                        .id(c.getCellId())
                                        .name(c.getCellName())
                                        .type(PropertyType.DOUBLE)
                                        .build()
                ).collect(Collectors.toList());
                return ServiceDescriptor.builder()
                        .serviceId(serviceId)
                        .name("社保公积金")
                        .path("/alps/" + ServiceProvider.SERVICE_ID.SIHF)
                        .descriptors(propertyDescriptors)
                        .build();
            default:
                return null;
        }
    }

    @Override
    @PostMapping(value = "/companyService/{service}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceDescriptor companyServiceDescriptor(String service, String companyId) {
        return new ServiceDescriptor();
    }
}
